﻿using System.Net;
using System.Text;
using HtmlAgilityPack;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System;
public class A3DataFetch
{
    string usr_name = "frank@gmail.com";
    string pwd = "12341122";
    static string logfile = "log.txt";
    public A3DataFetch(string usr_name, string pwd)
    {
        this.usr_name = usr_name;
        this.pwd = pwd;
    }
    public A3DataFetch()
    {
    }

    private string getNeed(string input, string begin, string end)
    {
        int iPos = input.IndexOf(begin);
        if (iPos > 0)
        {
            string s1 = input.Substring(iPos + begin.Length);
            int iPos2 = s1.IndexOf(end);
            if (iPos2 > 0)
            {
                return s1.Substring(0, iPos2);
            }
        }
        return null;
    }

    //结果写入文件filename
    public bool GetData(string filename)
    {
        string responseFromServer = null;
        Stream dataStream = null;
        StreamReader reader = null; 
        CookieContainer cc = new CookieContainer();
        
        log("begin getting data!");           
            
        try
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.dharamhk.com/searchstock.aspx");
            request.CookieContainer = cc;
            request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0";

            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            dataStream = response.GetResponseStream();
            reader = new StreamReader(dataStream, System.Text.Encoding.Default);
            responseFromServer = reader.ReadToEnd();
            response.Close();

            /*
            FileStream fs = new FileStream("a1.html", FileMode.Create);
            //获得字节数组
            byte[] output_data = new UTF8Encoding().GetBytes(responseFromServer);
            //开始写入
            fs.Write(output_data, 0, output_data.Length);
            //清空缓冲区、关闭流
            fs.Flush();
            fs.Close();*/

            request.Abort();

            request = (HttpWebRequest)WebRequest.Create("http://www.dharamhk.com/searchstock.aspx");
            request.Method = "POST";    //post
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cc;
            request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0";
            request.Referer = "http://www.dharamhk.com/searchstock.aspx";
            request.ServicePoint.Expect100Continue = false;

            string postData = null;
            ASCIIEncoding encoding = new ASCIIEncoding();

            string __VIEWSTATE = getNeed(responseFromServer, "id=\"__VIEWSTATE\" value=\"", "\" />");
            string crt = getNeed(responseFromServer, "id=\"txtCrt\" value=\"", "\" />");
            string __PREVIOUSPAGE = getNeed(responseFromServer, "id=\"__PREVIOUSPAGE\" value=\"", "\" />");
            __VIEWSTATE = HttpUtility.UrlEncode(__VIEWSTATE, encoding);
            crt = HttpUtility.UrlEncode(crt, encoding);
            __PREVIOUSPAGE = HttpUtility.UrlEncode(__PREVIOUSPAGE, encoding);
            postData = "__EVENTARGUMENT=";
            postData += "&";
            postData += "__EVENTTARGET=" + "&";
            postData += "__LASTFOCUS=" + "&";
            postData += "__PREVIOUSPAGE=" + __PREVIOUSPAGE + "&";
            postData += "__VIEWSTATE=" + __VIEWSTATE + "&";
            postData += "ctl00$ContentPlaceHolder1$dropDownList1=Select one" + "&";
            postData += "ctl00$ContentPlaceHolder1$drpDuration=7" + "&";
            postData += "ctl00$ContentPlaceHolder1$getsss=" + "&";
            postData += "ctl00$ContentPlaceHolder1$GridBasket=" + "&";
            postData += "ctl00$ContentPlaceHolder1$hdninBasket=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtAlertName=Alert Name" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtbasket=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtCrt=" + crt + "&";
            postData += "ctl00$ContentPlaceHolder1$txtloca=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtmaxweight=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtminweight=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtrefno=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtReportNo=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtsearchname=Search Name" + "&";
            postData += "ctl00$login_box$txtForgotL=" + "&";
            postData += "ctl00$login_box$txtLoginV.x=7" + "&";
            postData += "ctl00$login_box$txtLoginV.y=9" + "&";
            postData += "ctl00$login_box$txtUserNameV=" + usr_name + "&";
            postData += "ctl00$login_box$txtPasswordV=" + pwd + "&";
            postData += "ctl00$txtPass=" + "&";
            postData += "ctl00$txtUname=Email";

            byte[] data = encoding.GetBytes(postData);
            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            HttpWebResponse responsepost = (HttpWebResponse)request.GetResponse();

            Stream stream = responsepost.GetResponseStream();
            string result = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();

            /*
            FileStream fs2 = new FileStream("a2.html", FileMode.Create);
            //获得字节数组
            byte[] output_data2 = new UTF8Encoding().GetBytes(result);
            //开始写入
            fs2.Write(output_data2, 0, output_data2.Length);
            //清空缓冲区、关闭流
            fs2.Flush();
            fs2.Close();*/

            request.Abort();
            request = (HttpWebRequest)WebRequest.Create("http://www.dharamhk.com/searchstock.aspx");
            request.Method = "POST";    //post
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cc;
            request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0";
            request.Referer = "http://www.dharamhk.com/searchstock.aspx";
            request.ServicePoint.Expect100Continue = false;


            postData = null;
            postData = "__EVENTARGUMENT=";
            postData += "&";
            postData += "__EVENTTARGET=" + "&";
            postData += "__LASTFOCUS=" + "&";
            postData += "__PREVIOUSPAGE=" + __PREVIOUSPAGE + "&";
            postData += "__VIEWSTATE=" + __VIEWSTATE + "&";
            postData += "ctl00$ContentPlaceHolder1$dropDownList1=Select one" + "&";
            postData += "ctl00$ContentPlaceHolder1$drpDuration=7" + "&";
            postData += "ctl00$ContentPlaceHolder1$getsss=" + "&";
            postData += "ctl00$ContentPlaceHolder1$GridBasket=" + "&";
            postData += "ctl00$ContentPlaceHolder1$hdninBasket=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtAlertName=Alert Name" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtbasket=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtCrt=" + crt + "&";
            postData += "ctl00$ContentPlaceHolder1$txtloca=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtmaxweight=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtminweight=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtrefno=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtReportNo=" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtsearchname=Search Name" + "&";
            postData += "ctl00$login_box$txtForgotL=" + "&";
            postData += "ctl00$txtPass=" + "&";
            postData += "ctl00$ContentPlaceHolder1$btnDExcelAll=Export ALL To Excel" + "&";
            postData += "ctl00$ContentPlaceHolder1$txtPage=1" + "&";
            postData += "ctl00$txtUname=Email";

            data = encoding.GetBytes(postData);
            newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            responsepost = (HttpWebResponse)request.GetResponse();
            stream = responsepost.GetResponseStream();
            result = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
            FileStream fs3 = new FileStream(filename, FileMode.Create);
            //获得字节数组
            byte[] output_data3 = encoding.GetBytes(result);
            //开始写入
            fs3.Write(output_data3, 0, output_data3.Length);
            //清空缓冲区、关闭流
            fs3.Flush();
            fs3.Close();
        }
        catch (Exception ex)
        {
            log(ex.Message);
            log("Get File Error!");
            return false;
        }

        log("getting data success!");  
        return true;
    }

    public bool getDateFromXml(string strPath, List<A3Product> lstData)
    {

        log("begin parse data:");

        try
        {

            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.Load(strPath, Encoding.ASCII);
            HtmlNode hnTable = htmlDoc.DocumentNode.SelectSingleNode("/table[2]");//get the second table
            HtmlNodeCollection trNodes = hnTable.SelectNodes("tr[position()>1]");
            lstData.Clear();

            for (int y = 0; y < trNodes.Count; y++)
            {
                HtmlNodeCollection tdNodes = trNodes[y].SelectNodes("td");
                if (tdNodes.Count != 21)
                    return false;
                A3Product oA3Product = new A3Product();
                oA3Product.Shape = tdNodes[0].InnerText.Trim();
                oA3Product.Size = tdNodes[1].InnerText.Trim();
                oA3Product.Color = tdNodes[2].InnerText.Trim();
                oA3Product.Clarity = tdNodes[3].InnerText.Trim();
                oA3Product.Cut = tdNodes[4].InnerText.Trim();
                oA3Product.Polish = tdNodes[5].InnerText.Trim();
                oA3Product.Sym = tdNodes[6].InnerText.Trim();
                oA3Product.Flour = tdNodes[7].InnerText.Trim();
                oA3Product.M1 = tdNodes[8].InnerText.Trim();
                oA3Product.M2 = tdNodes[9].InnerText.Trim();
                oA3Product.M3 = tdNodes[10].InnerText.Trim();
                oA3Product.Depth = tdNodes[11].InnerText.Trim();
                oA3Product.Table = tdNodes[12].InnerText.Trim();
                oA3Product.Ref = tdNodes[13].InnerText.Trim();
                oA3Product.CertificateNo = tdNodes[14].InnerText.Trim();
                oA3Product.Detail = tdNodes[15].InnerText.Trim();
                oA3Product.Cert = tdNodes[16].InnerText.Trim();
                oA3Product.RapRate = tdNodes[17].InnerText.Trim();
                oA3Product.Discount = tdNodes[18].InnerText.Trim();
                oA3Product.Price = tdNodes[19].InnerText.Trim();
                oA3Product.ReportNo = tdNodes[20].InnerText.Trim();


                HtmlNode NodeHref = tdNodes[14].FirstChild;
                oA3Product.CertificateNoHref = NodeHref.GetAttributeValue("href", "");
                NodeHref = tdNodes[16].FirstChild;
                oA3Product.CertHref = NodeHref.GetAttributeValue("href", "");

                lstData.Add(oA3Product);
            }
        }
        catch (Exception ex)
        {
            log(ex.Message);
            log("Parse File Error!");
            return false;
        }

        log("Parse File Success!");
        return true;
    }

    public static void log(string Message)
    {
        System.DateTime curtime = new System.DateTime();
        curtime = System.DateTime.Now;
        string  StrTime = curtime.ToString();
        string output_data = StrTime + ": " + Message + "\n";
        UTF8Encoding encoding = new UTF8Encoding();
        FileStream fs = new FileStream(logfile, FileMode.Create|FileMode.Append);
        //获得字节数组
        byte[] data = encoding.GetBytes(output_data);
        //开始写入
        fs.Write(data, 0, data.Length);
        //清空缓冲区、关闭流
        fs.Flush();
        fs.Close();
    }
}